﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Globalization;
public partial class Main_Reports_Inventory_POStatusReport : System.Web.UI.Page
{
    Datautility DU = new Datautility(); DataTable Dt_detail = new DataTable();
    DataTable DtPOStatus = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //GetData();            
        }
    }
    protected void Fill_data()
    {
        if (txtfromdate.Text != "" && txttodate.Text != "")
        {
            lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
            SqlParameter[] Param = { new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                   new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))
                               };
            Dt_detail = DU.getdatausingprocedure("IVR_RPT_POSTATUS_REPORT", Param);
            if (Dt_detail.Rows.Count > 0)
            {
                grdViewPoStatus.DataSource = Dt_detail;
                grdViewPoStatus.Columns[6].FooterText = Dt_detail.AsEnumerable().Select(x => x.Field<decimal>("POQTY")).Sum().ToString("N0");                
                grdViewPoStatus.Columns[9].FooterText = Dt_detail.AsEnumerable().Select(x => x.Field<decimal>("POVALUE")).Sum().ToString();
                grdViewPoStatus.Columns[17].FooterText = Dt_detail.AsEnumerable().Select(x => x.Field<decimal>("INVOICEVALUE")).Sum().ToString();
                grdViewPoStatus.Columns[19].FooterText = Dt_detail.AsEnumerable().Select(x => x.Field<decimal>("PORECQTY")).Sum().ToString("N0");
                grdViewPoStatus.Columns[21].FooterText = Dt_detail.AsEnumerable().Select(x => x.Field<decimal>("PORECVALUE")).Sum().ToString();
                grdViewPoStatus.DataBind();
            }
            lblRecordNo.Text = grdViewPoStatus.Rows.Count.ToString() + " Record(s)";
            ddlColumnname.Style["display"] = "block";
            lblcolumnName.Style["display"] = "block";
            lblvalue.Style["display"] = "block";
            txtValue.Style["display"] = "block";
            btnSearch.Style["display"] = "block";
            imgbtnPrint.Style["display"] = "block";
            ImgBtnExcel.Style["display"] = "block";
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdViewPoStatus.Visible = true;
        Fill_data();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMCODE like'%" + txtValue.Text + "%'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMDESC Like'%" + txtValue.Text + "%'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For Purchase Order No
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "PONO Like'%" + txtValue.Text + "%'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For Currency
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "CURRENCY='" + txtValue.Text + "'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For Folio NO
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "FOLIONO Like'%" + txtValue.Text + "%'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Vendor Country
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "COUNTRY='" + txtValue.Text + "'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For Invoice Value
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "INVOICEVALUE='" + txtValue.Text + "'");
            grdViewPoStatus.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "8") //For Payment Terms
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "PAYMENTTERMS Like'%" + txtValue.Text + "%'");
            grdViewPoStatus.AllowPaging = false;
        } 
        else
        {
            Fill_data();
        }

        DataRow[] Dr = Dt_detail.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = Dt_detail.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdViewPoStatus.DataSource = Tempdt;
        grdViewPoStatus.DataBind();
    }    
    protected void grdViewPoStatus_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdViewPoStatus.PageIndex = e.NewPageIndex;
        Fill_data();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Purchase_Order_Status_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdViewPoStatus.ShowFooter = false;
            grdViewPoStatus.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdViewPoStatus.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        Fill_data();
    }    
    protected void OnDataBound(object sender, EventArgs e)
    {
        //for (int i = grdViewPoStatus.Rows.Count - 1; i > 0; i--)//For Rows
        //{
        //    GridViewRow row = grdViewPoStatus.Rows[i];
        //    GridViewRow previousRow = grdViewPoStatus.Rows[i - 1];
        //    for (int j = 3; j < row.Cells.Count; j++)//for Columns
        //    {
        //        if (row.Cells[j].Text == previousRow.Cells[j].Text)
        //        {
        //            if (previousRow.Cells[j].RowSpan == 0)
        //            {
        //                if (row.Cells[j].RowSpan == 0)
        //                {
        //                    previousRow.Cells[j].RowSpan += 2;
        //                }
        //                else
        //                {
        //                    previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
        //                }
        //                row.Cells[j].Visible = false;
        //            }
        //        }
        //    }
        //    #region
        //    //for (int j = 3; j < 4; j++)
        //    //{
        //    //    if (row.Cells[j].Text == previousRow.Cells[j].Text)
        //    //    {
        //    //        if (previousRow.Cells[j].RowSpan == 0)
        //    //        {
        //    //            if (row.Cells[j].RowSpan == 0)
        //    //            {
        //    //                previousRow.Cells[j].RowSpan += 2;
        //    //            }
        //    //            else
        //    //            {
        //    //                previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
        //    //            }
        //    //            row.Cells[j].Visible = false;
        //    //        }
        //    //    }
        //    //}
        //    //for (int k = 8; k < 9; k++)
        //    //{
        //    //    if (row.Cells[k].Text == previousRow.Cells[k].Text)
        //    //    {
        //    //        if (previousRow.Cells[k].RowSpan == 0)
        //    //        {
        //    //            if (row.Cells[k].RowSpan == 0)
        //    //            {
        //    //                previousRow.Cells[k].RowSpan += 2;
        //    //            }
        //    //            else
        //    //            {
        //    //                previousRow.Cells[k].RowSpan = row.Cells[k].RowSpan + 1;
        //    //            }
        //    //            row.Cells[k].Visible = false;
        //    //        }
        //    //    }
        //    //}
        //    #endregion
        //}
        //for (int r = grdViewPoStatus.Rows.Count - 1 ; r > 0; r++)
        //{
        //    GridViewRow row = grdViewPoStatus.Rows[r];
        //    GridViewRow previousrow = grdViewPoStatus.Rows[r - 1];
        //    for (int c = 3; c < row.Cells.Count; c++)
        //    {
        //        if (row.Cells[c].Text == previousrow.Cells[c].Text)
        //        {

        //        }
        //    }
        //}
    }
    protected void grdViewPoStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[6].Text != e.Row.Cells[19].Text)
            {
                //e.Row.Cells[17].Font.Size = 12;
                e.Row.Cells[19].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[19].Font.Bold = true;
            }            
        }
    }
}